﻿using Hetang.Areas.User.Models.Classes;
using Hetang.Areas.User.Models.Helper;
using Hetang.Models.Authorize;
using Hetang.Models.DataContext;
using Hetang.Models.Helper;
using Hetang.Models.Interface;
using Hetang.Models.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Hetang.Areas.User.Controllers
{
    [Authorize(Users = "person")]
    [CustomAuthorizeAttribute]
    public class PersonController : Controller
    {
        IDatabaseRepository _dr;
        ILoginRepository _lr;
        IConfigRepository _cr;
        ISessionRepository _sr;

        public PersonController() : this(new DatabaseRepository(), new LoginRepository(), new ConfigRepository(),new SessionRepository()) { }

        public PersonController(IDatabaseRepository dr, ILoginRepository lr, IConfigRepository cr, ISessionRepository sr)
        {
            _dr = dr;
            _lr = lr;
            _cr = cr;
            _sr = sr;
        }
        
        public ActionResult Index()
        {
            tbl_Resume resume = _dr.GetResume();

            if (resume != null)
            {
                dt_Resume person = new dt_Resume();

                resume.Letter = resume.Letter.Replace("<br />", "\r\n");
                person.resume = resume;
                person.BornYear = resume.Born.Year;
                person.BornMonth = resume.Born.Month;
                person.BornDay = resume.Born.Day;

                return View(person);
            }
            else
                return View();
        }

        [HttpPost]
        public ActionResult Index(dt_Resume person, string[] EnglishList)
        {
            TempData.Clear();

            if (ModelState.IsValid)
            {
                try
                {
                    person = new ResumeHelper().HandleResume(person, EnglishList);                    

                    if (_sr.GetResumeId() != 0)
                    {                        
                        _dr.UpdateResume(person.resume);
                    }
                    else
                    {
                        person.resume.Modify_Date = DateTime.Now;
                        person.resume.Add_Date = DateTime.Now;
                        _dr.AddResume(person.resume);
                    }

                    TempData.Add("Info", "操作成功！");

                    return RedirectToAction("index");
                }
                catch
                {
                    TempData.Add("Error","系统繁忙，请稍后再试！");
                    return View(person);
                }
            }
            else
                return View(person);
        }
        
        public ActionResult Create()
        {
            if (_dr.GetResume() != null)
                return RedirectToAction("index");
            else
            {
                dt_Resume person = new dt_Resume()
                {
                    resume = new tbl_Resume() { }
                };
                return View(person);
            }
        }

        [HttpPost]
        public ActionResult Create(dt_Resume person, string[] EnglishList)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    person = new ResumeHelper().HandleResume(person, EnglishList);

                    person.resume.Add_Date = DateTime.Now;

                    _dr.AddResume(person.resume);
                    _dr.Save();

                    return RedirectToAction("index");
                }
                catch
                {                    
                    return RedirectToAction("error500", "error", new { area = "" });
                }
            }
            else
                return View(person);            
        }

        public ActionResult Photo()
        {
            return View();
        }
    }
}
